<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */

namespace app\api\model;

use think\Model;
//use traits\model\SoftDelete;

class Base extends Model{

    //每页条数
    protected $limit = 10;

    //use SoftDelete;

    //protected $autoWriteTimestamp = true;
    //protected $deleteTime = 'delete_time';

    protected $order = ["id" => "desc", "create_time" => "desc"];

    //基础查询时间
    /*protected function base($query){
        $query->whereNull('delete_time');// 添加软删除条件
    }*/

    public function total($where = array()){
        return self::where($where)->count();
    }

    public function getList($where=array()){
        return self::where($where)->order($this->order)->paginate($this->limit, false);
    }

    public function queryData($where=array(), $field = ''){
        return self::field($field)->where($where)->order($this->order)->select();
    }

    public function findDataByID($id, $field = ''){
        return self::where('id',$id)->field($field)->find();
    }

    public function findData($where = array(), $field = ''){
        return self::where($where)->field($field)->find();
    }

    /**
     * 获取详情 查找单条记录
     */
    public static function detail($where, array $with = [])
    {
        try {
            $query = (new static)->with($with);
            return is_array($where) ? $query->where($where)->find() : $query->find((int)$where);
        } catch (\Exception $e) {
            return null;
        }
    }

    public function saveData($data, $isUpdate, $where = array()){
        $time = time();
        $data['update_time'] = $time;
        if(!$isUpdate){
            $data['create_time'] = $time;
        }
        return self::allowField(true)->isUpdate($isUpdate)->save($data, $where);
    }

    public function updateById($id, $data) {
        $data['update_time'] = time();
        return self::allowField(true)->where(["id" => $id])->update($data);
    }
    
   
    //根据条件软删除
    public function delDataByWhere($where = array()){
        return self::destroy($where);
    }

    //根据id软删除
    public function delDataByID($id){
        return self::destroy($id);
    }

    protected function prefixImageUrl($value){
        return config("setting.domain_prefix").config("setting.image_prefix").$value;
    }

    public function getNormalInIds($ids) {
        return $this->whereIn("id", $ids)
            ->where("status", "=", 1)
            ->select();
    }



    /**
     * 合并设置项
     * @param array $confusion
     * @param array $variable
     * @param string $_
     * @param bool $__
     * @return array
     */
    protected static function reorganize(array $confusion, array $variable, string $_ = 'cache', bool $__ = false): array
    {
        return self::reorganizeb($confusion, $variable, $_, $__);
    }

    /**
     * 合并设置项
     * @param array $array1 系统默认配置
     * @param array $array2 用户自定义
     * @param string $_ 数据来源[后期加载附加数据]
     * @param false $__ 是否只显示value
     * @return array
     */
    public static function reorganizeb(array $array1, array $array2, string $_ = 'cache', bool $__ = false): array
    {
        if (!in_array($_, ['cache', 'app'])) {
            return [];
        }
        $caching = [];
        foreach (($array1 + $array2) as $key => $val) {
            $caching[$key] = (isset($array1[$key]) && is_array($array1[$key]) && isset($array2[$key]) && is_array($array2[$key]))
                ? (is_assoc($array1[$key]) ? self::reorganize($array1[$key], $array2[$key], $_, $__) : $array2[$key])
                : ($array2[$key] ?? $array1[$key]);
        }
        return $__ === true ? self::getValues($caching, false) : $caching;
    }


    /**
     * 仅返回values数据
     * @param array $setting 全部设置
     * @param bool $setKey 是否设置键值
     * @return array
     */
    public static function getValues(array $setting, bool $setKey = true): array
    {
        $data = [];
        foreach ($setting as $k => $item) {
            $setKey ? $data[$item['key']] = $item['values'] : $data[$k] = $item;
        }
        return $data;
    }

}